package cn.itenzo.good.service.impl;

import cn.itenzo.good.dao.GoodItemMapper;
import cn.itenzo.good.model.GoodItem;
import cn.itenzo.good.service.ItemService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/5/18 0018.
 */
@Component("itemService")
@Service(version = "1.0.0")
public class ItemServiceImpl implements ItemService {

    private static final Logger logger = LoggerFactory.getLogger(ItemServiceImpl.class);

    @Autowired
    private GoodItemMapper goodItemMapper;


    @Override
    public List<GoodItem> queryItemByStoreId(Long storeId) {
        logger.info("查询商品分类  storeId ==> {}",storeId);
        GoodItem goodItem = new GoodItem();
        goodItem.setmStoreid(storeId);
        return goodItemMapper.select(goodItem);
    }


    @Override
    public GoodItem queryItemById(Long id) {
        logger.info("查询商品分类  ID==> {}",id);
        return goodItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer deleteItemById(Long id) {
        logger.info("删除商品分类  ID==> {}",id);
        return goodItemMapper.deleteByPrimaryKey(id);
    }


    @Override
    public Integer save(GoodItem entity) {
        entity.setCtime(new Date());
        return goodItemMapper.insertSelective(entity);
    }


    @Override
    public PageInfo<GoodItem> queryItemByPage(GoodItem entity, Integer page, Integer rows,String orderClause) {
        logger.info("分页查询分类");
        PageHelper.startPage(page,rows);
        Example example = new Example(GoodItem.class);

        example.setOrderByClause(orderClause);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mStoreid",entity.getmStoreid());

        List<GoodItem> select = goodItemMapper.selectByExample(example);
        PageInfo info = new PageInfo<>(select);
        return info;
    }
}
